package com.ruoyi.tool.service.impl;

import com.ruoyi.tool.service.IPDFToolsService;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.graphics.image.LosslessFactory;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.springframework.stereotype.Service;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

@Service
public class PDFToolsServiceImpl implements IPDFToolsService {
    @Override
    public void leftRightSplit(String pdfName) {

        String inputPath = pdfName;
        String outputPath = inputPath.replace(".pdf","_output.pdf");

        try (PDDocument inputDocument = PDDocument.load(new File(inputPath))) {
            PDDocument outputDocument = new PDDocument();
            PDFRenderer renderer = new PDFRenderer(inputDocument);

            for (int pageIndex = 0; pageIndex < inputDocument.getNumberOfPages(); pageIndex++) {
                BufferedImage image = renderer.renderImageWithDPI(pageIndex, 300); // PDFBox 3.x 使用 renderImage()
                int width = image.getWidth();
                int height = image.getHeight();
                int halfWidth = width / 2;


                // 分割左右图像
                BufferedImage leftImage = image.getSubimage(0, 0, halfWidth, height);
                BufferedImage rightImage = image.getSubimage(halfWidth, 0, halfWidth, height);


                PDPage leftPage = new PDPage(new PDRectangle(halfWidth, height));
                try (PDPageContentStream contentStream = new PDPageContentStream(outputDocument, leftPage)) {
                    //  PDImageXObject leftImageXObject = PDImageXObject.createFromFileByContent(leftImage, outputDocument);
                    PDImageXObject leftImageXObject = LosslessFactory.createFromImage(outputDocument, leftImage);
                    contentStream.drawImage(leftImageXObject, 0, 0, halfWidth, height);
                }

                PDPage rightPage = new PDPage(new PDRectangle(halfWidth, height));
                try (PDPageContentStream contentStream = new PDPageContentStream(outputDocument, rightPage)) {
                    // PDImageXObject rightImageXObject = PDImageXObject.createFromFileByContent(rightImage, outputDocument);

                    PDImageXObject rightImageXObject = LosslessFactory.createFromImage(outputDocument, rightImage);
                    contentStream.drawImage(rightImageXObject, 0, 0, halfWidth, height);
                }

                outputDocument.addPage(leftPage);
                outputDocument.addPage(rightPage);
            }

            outputDocument.save(outputPath);
            System.out.println("PDF 分割完成，输出文件: " + outputPath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 修复坐标系：将 BufferedImage 上下翻转
    private BufferedImage flipImageVertically(BufferedImage image) {
        int width = image.getWidth();
        int height = image.getHeight();
        BufferedImage flipped = new BufferedImage(width, height, image.getType());

        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                // 将 (x, y) 映射到 (x, height-1-y)
                flipped.setRGB(x, height - 1 - y, image.getRGB(x, y));
            }
        }
        return flipped;
    }
}
